/*
 * 文件名:CustomerServiceImpl.java
 * 创建时间:2010-10-01
 * 版本:2.0
 * 版权所有:杨涛 walle1027@gmail.com
 */
package com.yangtao.wms.master.customer.service.impl;

import com.yangtao.framework.hibernate.Selector;
import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.wms.dao.CustomerDao;
import com.yangtao.wms.dao.CustomerDetailDao;
import com.yangtao.wms.dao.CustomerSettingDao;
import com.yangtao.wms.domain.Customer;
import com.yangtao.wms.domain.CustomerDetail;
import com.yangtao.wms.domain.CustomerSetting;
import com.yangtao.wms.master.customer.service.CustomerService;
import com.yangtao.wms.master.customer.web.CustomerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 代码生成器 v-2.0
 * @version 2.0
 */
@Service()
public class CustomerServiceImpl extends BaseService implements CustomerService {

	@Autowired
	protected CustomerDao customerDao;
	@Autowired
	protected CustomerDetailDao customerDetailDao;
	@Autowired
	protected CustomerSettingDao customerSettingDao;

	/**
	 * 查询记录，并且分页
	 *
	 * @param model 页面参数
	 */
	@Override
	public CustomerModel list(CustomerModel model) {
		Selector selector = new Selector();
		selector.add(".customerDetail");
		selector.add(".customerSetting");
		customerDao.findPageByCondition(selector, model.getPage(), model.getCondition());
		return model;
	}

	/**
	 * 取得唯一记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public CustomerModel loadCustomer(CustomerModel model) {
		if (StringHelper.isNotEmpty(model.getCustomer().getId())) {
			Selector selector = new Selector();
			selector.add(".customerDetail");
			selector.add(".customerSetting");
			model.setCustomer(customerDao.loadForReview(model.getCustomer().getId(), selector));
		}
		return model;
	}

	/**
	 * 新增记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public CustomerModel addCustomer(CustomerModel model) {
		//VO
		Customer customerVO = model.getCustomer();
		//PO
		Customer customerPO = new Customer();
		mapper.map(customerVO, customerPO);
		//构造关联从对象
		//构造关联对象
		CustomerDetail customerDetailVO = customerVO.getCustomerDetail();
		CustomerDetail customerDetailPO = new CustomerDetail();
		mapper.map(customerDetailVO, customerDetailPO);
		//设置额外属性
		setCustomerDetailProperties(customerDetailPO, model);
		//构造关联对象关联关系
		customerDetailPO.setCustomer(customerPO);
		customerPO.setCustomerDetail(customerDetailPO);
		//构造关联对象
		CustomerSetting customerSettingVO = customerVO.getCustomerSetting();
		CustomerSetting customerSettingPO = new CustomerSetting();
		mapper.map(customerSettingVO, customerSettingPO);
		//设置额外属性
		setCustomerSettingProperties(customerSettingPO, model);
		//构造关联对象关联关系
		customerSettingPO.setCustomer(customerPO);
		customerPO.setCustomerSetting(customerSettingPO);
		//保存对象
		customerDao.save(customerPO);
		//增加保存成功的消息
		model.addInfo("add.success");
		return model;
	}

	/**
	 * 更新记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public CustomerModel updateCustomer(CustomerModel model) {
		//VO
		Customer customerVO = model.getCustomer();
		//主键
		String customerId = customerVO.getId();
		//PO
		Customer customerPO = customerDao.load(customerId);
		//复制页面属性
		mapper.map(customerVO, customerPO);
		//TODO 增加其他属性
		//TODO 更新关联对象
		//构造关联从对象
		//构造关联对象，一对一的情况下，假定它不会更新关联关系
		String customerDetailId = customerVO.getCustomerDetail() == null ? null : customerVO.getCustomerDetail().getId();
		CustomerDetail customerDetailPO = customerDetailDao.load(customerDetailId);
		if (customerDetailPO != null) {
			//根据页面展示项 copy字段，此处要留有扩展点
			mapper.map(customerVO.getCustomerDetail(), customerDetailPO);
			//增加其他属性
			setCustomerDetailProperties(customerDetailPO, model);
			//构造关联对象关联关系
			customerDetailPO.setCustomer(customerPO);
		}

		//构造关联对象，一对一的情况下，假定它不会更新关联关系
		String customerSettingId = customerVO.getCustomerSetting() == null ? null : customerVO.getCustomerSetting().getId();
		CustomerSetting customerSettingPO = customerSettingDao.load(customerSettingId);
		if (customerSettingPO != null) {
			//根据页面展示项 copy字段，此处要留有扩展点
			mapper.map(customerVO.getCustomerSetting(), customerSettingPO);
			//增加其他属性
			setCustomerSettingProperties(customerSettingPO, model);
			//构造关联对象关联关系
			customerSettingPO.setCustomer(customerPO);
		}

		//增加保存成功的消息
		model.addInfo("update.success");
		return model;
	}

	/**
	 * 删除记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public CustomerModel deleteCustomer(CustomerModel model) {
		customerDao.delete(model.getCustomer().getId());
		model.addInfo("delete.success");
		return model;
	}

	/**
	 * 设置关联对象:客户详细信息表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param customerDetail 关联对象
	 * @param model          页面参数类
	 */
	protected void setCustomerDetailProperties(final CustomerDetail customerDetail, final CustomerModel model) {
	}

	/**
	 * 设置关联对象:客户设置信息表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param customerSetting 关联对象
	 * @param model           页面参数类
	 */
	protected void setCustomerSettingProperties(final CustomerSetting customerSetting, final CustomerModel model) {
	}
}
